<?php

class CategoriesController extends Controller
{
	public function actionIndex()
	{
		$model=new Categories('search');
		$model->unsetAttributes();
		if(isset($_GET['Categories']))
			$model->attributes=$_GET['Categories'];

		$this->render('index', array(
			'model'=>$model
		));
	}
    
    public function actionCreate()
    {
        $model = new Categories;
        $seotool = new SeoTools;
        
        if (isset($_POST['Categories'])) {
            $model->attributes = $_POST['Categories'];
            $model->url = $this->createSlug($model->title);
            $model->image = '';
            if ($model->save()) {
                $seotool->attributes = $_POST['SeoTools'];
                $seotool->metarobot = '';
                $seotool->tables = 'categories';
                $seotool->table_id = $model->id;
                $seotool->status = 'A';
                $seotool->posteddate = date('Y-m-d H:i:s');
                $seotool->save();
            }
            
            $this->redirect(array('/Events/categories/index'));
        }
        
        $this->render('create', array(
            'model'=>$model,
            'seotool'=>$seotool
        ));
    }
    
    public function actionUpdate($id)
    {
        $model = $this->loadModel($id);
        
        if(isset($_POST['Categories']))
		{
			$model->attributes = $_POST['Categories'];
            if ($model->save()) {
                $model->seotool->attributes = $_POST['SeoTools'];
                $model->seotool->save();
            }
            
            $this->redirect(array('/Events/categories/index'));
        }
        
        $this->render('update', array(
            'model'=>$model
        ));
    }
    
    public function actionDelete($id)
	{
		//$this->loadModel($id)->delete();
        $model = $this->loadModel($id);
        
        $model->updateByPk($id, array('status'=>'I'));

		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
	}
	
	public function loadModel($id)
	{
		$model= Categories::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}
}